package com.video4s.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.video4s.entity.UserYoutubeEntity;

public class UserYoutubeImplDAO extends BaseDAO implements UserYoutubeDAO {

	//private final static String INSERT = "INSERT INTO UserYoutube(UserYoutubeId,UserYoutubeLink) VALUES (?,?)";
	private final static String INSERT = "EXEC INSERT_USERYOUTUBE_ROBOTS ?,?";
	private final static String SELECT_ONE = "SELECT * FROM UserYoutube WHERE UserYoutubeLink = ?";
	private final static String SELECT_TOP_PROCESS = "SELECT TOP %s * FROM UserYoutube WHERE UserYoutubeProcess = ? ORDER BY UserYoutubeDate ASC";
	private final static String UPDATE_PROCESS = "UPDATE UserYoutube SET UserYoutubeProcess = ? WHERE UserYoutubeId = ?";
	@Override
	public void onInsert(UserYoutubeEntity entity) throws Exception {
		// TODO Auto-generated method stub
		try {
			//if(!onSelectOne(entity.getUserYoutubeLink())) {
				getConnection();
				PreparedStatement preparedStatement2 = conn.prepareStatement(INSERT);
				preparedStatement2.setString(1, entity.getUserYoutubeId());
				preparedStatement2.setString(2, entity.getUserYoutubeLink());
				preparedStatement2.executeUpdate();
				preparedStatement2.close();
			//}
			
		} catch (Exception e) {
			throw e;
		} finally {
			close();
		}
	}
	@Override
	public boolean onSelectOne(String userYoutubeLink) throws Exception {
		try {
			getConnection();
			PreparedStatement pStmt1 = conn.prepareStatement(SELECT_ONE);
			pStmt1.setString(1, userYoutubeLink);
			ResultSet resultSet = pStmt1.executeQuery();
			if(resultSet.next()) {
				return true;
			}
			resultSet.close();
			pStmt1.close();
		} catch (Exception e) {
			throw e;
		} finally {
			close();
		}
		return false;
	}
	@Override
	public ArrayList<UserYoutubeEntity> onSelectTop(int top,int process)
			throws Exception {
		ArrayList<UserYoutubeEntity> listUser = new ArrayList<UserYoutubeEntity>();
		try {
			getConnection();
			PreparedStatement pStmt1 = conn.prepareStatement(String.format(SELECT_TOP_PROCESS, top));
			pStmt1.setInt(1, process);
			ResultSet resultSet = pStmt1.executeQuery();
			while(resultSet.next()) {
				UserYoutubeEntity userYoutubeEntity = new UserYoutubeEntity();
				userYoutubeEntity.setCategoryId(0);
				userYoutubeEntity.setUserYoutubeId(resultSet.getString("UserYoutubeId"));
				userYoutubeEntity.setUserYoutubeLink(resultSet.getString("UserYoutubeLink"));
				userYoutubeEntity.setCategoryId(resultSet.getInt("CategoryId"));
				listUser.add(userYoutubeEntity);
				PreparedStatement preparedStatement2 = conn.prepareStatement(UPDATE_PROCESS);
				preparedStatement2.setInt(1, 1);
				preparedStatement2.setString(2, resultSet.getString("UserYoutubeId"));
				preparedStatement2.executeUpdate();
				preparedStatement2.close();
			}
			resultSet.close();
			pStmt1.close();
		} catch (Exception e) {
			throw e;
		} finally {
			close();
		}
		return listUser;
	}
	
	@Override
	public boolean onUpdateProcess(int process, String id) throws Exception {
		try {
			getConnection();
			PreparedStatement preparedStatement2 = conn.prepareStatement(UPDATE_PROCESS);
			preparedStatement2.setInt(1, process);
			preparedStatement2.setString(2, id);
			int flag = preparedStatement2.executeUpdate();
			preparedStatement2.close();
			if(flag > 0) {
				return true;
			}
		} catch (Exception e) {
			throw e;
		} finally {
			close();
		}
		return false;
	}
	

}
